﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace Pipo.LblGenExtensions
    {
    public class EntityOps
        {
        public static object Clone ( object objecttoclone){
        // fetch data in myCustomer entity. (a graph). 
        // BinaryFormatter is the formatter for remoting, part of .NET
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream();
        formatter.Serialize(stream, objecttoclone);
        // seek the stream back to the beginning.
        stream.Seek(0,SeekOrigin.Begin);

        // deserialize the data back into a new instance (and all the related entities)
            object deserialize = formatter.Deserialize(stream);
            return deserialize;
        }

        }
    public class ErrorHandler{
        public  static  string GetUserMessage( Exception ex){
            //Exception ex = exs.InnerException;
            string usermsg = null;
                if ( ex is ORMConcurrencyException ){
                    usermsg = "A concurrency violation was detected. Please try to refresh your list or close and re-open the module.";}
                else if ( ex is ORMEntityIsDeletedException){
                    usermsg = "The record you selected could not be found.";}
                else if ( ex is ORMEntityOutOfSyncException){
                    usermsg = "Please try to refresh your list or close the window then try again.";}
                else if ( ex is ORMEntityValidationException){
                    usermsg = "Please check the details of the record and try again.";}
                else if ( ex is ORMFieldIsNullException){
                    usermsg = "A field field was not supplied. \n Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMFieldIsReadonlyException){
                usermsg = "Unexpected operation. \n Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMGeneralOperationException){
                    usermsg = "An error occured. \n Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMInheritanceInfoException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMInterpretationException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMInvalidFieldReadException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMLockTimeoutException){
                    usermsg = "SERVER TIMEOUT. You may try again. \n Please check your Network and Internet Connection. \n If the problem persists... contact your Network Administrator ";}
                else if ( ex is ORMQueryConstructionException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMQueryExecutionException){
                usermsg = "There was a problem saving the record.\n" + "Please code you supplied already exits.\n" ;}
                else if ( ex is ORMRelationException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMSecurityException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMSerializationOptimizationException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else if ( ex is ORMValueTypeMismatchException){
                usermsg = "Please try again. If this message continues, report this to your Supervisor.";}
                else{usermsg = "Please try to refresh your list or close and re-open the module. \n If this message continues, report this to your Supervisor.";}
            return string.Format("{0} \n  ", usermsg);
        }
    }
    }
